package eSn.Graphics;

import java.awt.Color;
import eSn.Core.*;

public class eSnColor {
	public byte Red;
	public byte Green;
	public byte Blue;
	public byte Alpha;          //透明值,范围0-255,0为透明,255为不透明
	
	
	/**
	 * 初始化一个黑色透明色
	 */
	public eSnColor(){
		this.Red = 0;
		this.Green = 0;
		this.Blue = 0;
		this.Alpha = 0;
	}
	
	/**
	 * 通过一个整型初始化一个色彩
	 */
	public eSnColor(int obj){
		Color c = new Color(obj,true);
		this.Red = (byte)c.getRed();
		this.Green = (byte)c.getGreen();
		this.Blue = (byte)c.getBlue();
		this.Alpha = (byte)c.getAlpha();
	}
	/**
	 * 通过一个整型初始化一个灰度色彩
	 */
	public eSnColor(byte obj){
		this.Red = obj;
		this.Green = obj;
		this.Blue = obj;
	}
	
	/**
	 * 通过基色初始化一个StdColor
	 */	
	public eSnColor(byte r,byte g,byte b,byte a){
 
		this.Red = r;
		this.Green = g ;
		this.Blue =b;
		this.Alpha = a;
	}
	/**
	 * 通过一个Java Color初始化一个StdColor
	 */
	public eSnColor(Color color){
 		fromColor(color);
	}
	
	
	
	public void fromColor(Color color){
		this.Red =  Num.toByte(color.getRed());
		this.Green =  Num.toByte(color.getGreen());
		this.Blue =  Num.toByte(color.getBlue());
		this.Alpha =  Num.toByte(color.getAlpha());
	}
	
	/**
	 * 将StdColor转成Java Color
	 */
	public Color toColor(){
		return new Color(this.Red, this.Green, this.Blue, this.Alpha);
	}
	
	public void fromByte(int r,int g,int b,int a){
		this.Red =  Num.toByte(r);
		this.Green =  Num.toByte(g);
		this.Blue =  Num.toByte(b);
		this.Alpha =  Num.toByte(a);
	}
	
	/**
	 * 取得当前eSnColor的灰度值
	 */
	public byte getGray(){
		//Y  = ( 0.29900 * R + 0.58700 * G + 0.11400 * B )/1
		double g = 0;
		g = 0.299 * this.Red;
		g = 0.587 * this.Green + g;
		g = 0.114 * this.Blue + g;
		long c = java.lang.Math.round(g);
		if (c > 255){
			return (byte)255 ; 
		} else {
			return (byte)c;
		}
	}

	/**
	 * 将当前色彩转成灰度
	 */
	public void toGray(){
		if ( this.Red  == this.Green && this.Red == this.Blue && this.Green == this.Blue ){
			return ;
		}else{
			byte i = getGray();
			this.Red = i;
			this.Green = i;
			this.Blue = i;
		}
	}
	
	
}
